Redirect all access requests to a domain and subdomain(s) except from specific IP address? [closed]

Posted by Christopher on Pro Webmasters See other posts from Pro Webmasters or by Christopher
Published on 2011-06-22T03:00:54Z Indexed on 2011/06/22 8:31 UTC
Read the original article Hit count: 645

This is a self-answered question... After much wrangling I found the magic combination of mod_rewrite rules so I'm posting here.

My scenario is that I have two domains - domain1.com and domain2.com - both of which are currently serving identical content (by way of a global 301 redirect from domain1 to domain2). Domain1 was then chosen to be repurposed to be a 'portal' domain - with a corporate CMS-based site leading off from the front page, and the existing 'retail' domain (domain2) left to serve the main web site.

In addition, a staging subdomain was created on domain1 in order to prepare the new corporate site without impinging on the root domain's existing operation. I contemplated just rewriting all requests to domain2 and setting up the new corporate site 'behind the scenes' without using a staging domain, but I usually use subdomains when setting up new sites.

Finally, I required access to the 'actual' contents of the domains and subdomains - i.e., to not be redirected like all other visitors - in order that I can develop the new site and test it in the staging environment on the live server, as I'm not using a separate development webserver in this case. I also have another test subdomain on domain1 which needed to be preserved.

The way I eventually set it up was as follows: (10.2.2.1 would be my home WAN IP)

.htaccess in root of domain1

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^10\.2\.2\.1
RewriteCond %{HTTP_HOST} !^staging.domain1.com$ [NC]
RewriteCond %{HTTP_HOST} !^staging2.domain1.com$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [R=301]

.htaccess in staging subdomain on domain1:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^10\.2\.2\.1
RewriteCond %{HTTP_HOST} ^staging.revolver.coop$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [R=301,L]

The multiple .htaccess files and multiple rulesets require more processing overhead and longer iteration as the visitor is potentially redirected twice, however I find it to be a more granular method of control as I can selectively allow more than one IP address access to individual staging subdomain(s) without automatically granting them access to everything else. It also keeps the rulesets fairly simple and easy to read. (or re-interpret, because I'm always forgetting how I put rules together!)

If anybody can suggest a more efficient way of merging all these rules and conditions into just one main ruleset in the root of domain1, please post! I'm always keen to learn, this post is more my attempt to preserve this information for those who are looking to redirect entire domains for all visitors except themselves (for design/testing purposes) and not just denying specific file access for maintenance mode (there are many good examples of simple mod_rewrite rules for 'maintenance mode' style operation easily findable via Google).

You can also extend the IP address detection - firstly by using wildcards ^10\.2\.2\..*: the last octet's \..* denotes the usual "." and then "zero or more arbitrary characters", signified by the .* - so you can specify specific ranges of IPs in a subnet or entire subnets if you wish.

You can also use square brackets: ^10\.2\.[1-255]\.[120-140]; ^10\.2\.[1-9]?[0-9]\.; ^10\.2\.1[0-1][0-9]\. etc.

The third way, if you wish to specify multiple discrete IP addresses, is to bracket them in the style of ^(1.1.1.1|2.2.2.2|3.3.3.3)$, and you can of course use square brackets to substitute octets or single digits again.

NB: if you're using individual RewriteCond lines to specify multiple IPs / ranges, make sure to put [OR] at the end of each one otherwise mod_rewrite will interpret as "if IP address matches 1.1.1.1 AND if IP address matches 2.2.2.2... which is of course impossible! However as far as I'm aware this isn't necessary if you're using the ! negator to specify "and is not...".

Kudos also to SE: this older question also came in useful when I was verifying my own knowledge prior to my futzing around with code. This page was helpful, as were the various other links posted below (can't hyperlink them all due to spam protection... other regex checkers are available). The AddedBytes cheat sheet's useful to pin up on your wall.

Other referenced URLs:

  • internetofficer.com/seo-tool/regex-tester/
  • fantomaster.com/faarticles/rewritingurls.txt
  • internetofficer.com/seo-tool/regex-tester/
  • addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/

© Pro Webmasters or respective owner

Related posts about htaccess

Related posts about redirects